//
//  Login_Vm.swift
//  LocaLink
//
//  Created by Icy on 2017/1/11.
//  Copyright © 2017年 Icy. All rights reserved.
//


import RxSwift
import Result
import SwiftyJSON
import NetWorkCore
import ModelProtocol

/*
 owner:cy
 update:2016年12月01日11:30:063
 info: 审核详情 的viewmodel
 */
class Login_Vm {
    
    /*
     接收返回的数据
     */
    let loginSubject = PublishSubject<(username:String,password:String)>()
    let loginSuccess = PublishSubject<JSON>()
    
    let registerSubject = PublishSubject<[String: AnyObject]>()
    let registerSuccess = PublishSubject<JSON>()
    /*
     通用设置
     */
    /// 表示正在加载更多种..
    let loading : Observable<Bool>
    /// 表示正在刷新中
    let refreshing = Variable(false)
    /// 保存错误
    let error = PublishSubject<MikerError>()
    private let disposeBag = DisposeBag()
    
    init() {
        let requestIndicator = ActivityIndicator()
        self.loading = requestIndicator.asObservable()

        
        loginSubject
            .map{ item in UserApi.login(username: item.username, password: item.password) }
            .emeRequestApiForJson(requestIndicator)
            .subscribe(onNext: { (result) in
                switch result {
                case .success(let rel):
                    self.loginSuccess.onNext(rel)
                case .failure(let error):
                    self.error.onNext(error)
                }
            })
            .disposed(by: disposeBag)

        
        registerSubject
            .map{ param in UserApi.signup(param: param) }
            .emeRequestApiForJson(requestIndicator)
            .subscribe(onNext: { (result) in
                switch result {
                case .success(let rel):
                    self.registerSuccess.onNext(rel)
                case .failure(let error):
                    self.error.onNext(error)
                }
            })
            .disposed(by: disposeBag)
    }
    
}
